<?php

class CpanelaccountController extends ControllerModuleWhm
{
    public function actionIndex()
    {
    	$now = time();
    	$id_account = app()->request->getParam('id_account', null);
    	if($id_account){
    		$whmaccount = $this->loadModelByPk($id_account, 'WhmAccount');
    		
    		if(!app()->user->hasAccountAccess($id_account)){
				$this->redirect(url('default/noaccess'));
			}
    		
    		$reload = app()->request->getParam('reload', 0);
    		$errors = null;
    		if($reload != 0){
    			$ret = $whmaccount->reLoadCpanelAccounts()
    				&& $whmaccount->reLoadFunctions();
    			
    			if($ret){
					//TODO: do not redirect, find out the reason why $whmaccount->CpanelAccounts is not updated after refresh
					app()->request->redirect(url('whm/cpanelaccount/index', array('id_account' => $id_account)));
				}else{
					$errors = $whmaccount->getErrors();
				}
    		}
    		
    		$this->render('index', array(
    			'sort' => app()->request->getParam('sort', 'domain_sort'),
				'whmaccount' => $whmaccount,
    			'errors' => $errors
			));
    	}
    }
    
	public function actionDetail($id_cpanel_accounts)
	{
		$id_cpanel_accounts = app()->request->getParam('id_cpanel_accounts', null);
		if($id_cpanel_accounts){
			if(app()->request->isAjaxRequest){
				$cpanelAccounts = $this->loadModelByPk($id_cpanel_accounts, 'CpanelAccounts');
				$this->_returnAjax(
					$cpanelAccounts->attributes
				);
			}
		}
	}
	
    public function actionSearchResult()
    {
    	$CpanelAccountsSearchForm = new CpanelAccountsSearchForm();

		$postData = app()->request->getPost(get_class($CpanelAccountsSearchForm), null);
		if($postData){
			$CpanelAccountsSearchForm->attributes = $postData;
			$cpanelAccounts = $CpanelAccountsSearchForm->search();
		}
		
		if(app()->request->isAjaxRequest){
			$this->_returnAjax(
				empty($cpanelAccounts)? array() : CHtml::listData($cpanelAccounts, 'id_cpanel_accounts', 'domain')
			);
		}else{
			$this->render('searchresult', array(
		    	'conditionField' => $CpanelAccountsSearchForm->conditionField,
		    	'conditionVal' => $CpanelAccountsSearchForm->conditionVal,
		    	'cpanelAccounts' => $cpanelAccounts
		    ));
		}
    }
    
	public function actionPasswords()
	{
		$now = time();
		if(app()->request->isAjaxRequest){
			$id_cpanel_account = app()->request->getPost('id_cpanel_account', null);
			$newpass = app()->request->getPost('newpass', null);
			$change_mysql_pass = app()->request->getPost('change_mysql_pass', 0);
			$ret = false;
			$passwd = $errors = null;
			
			$cpanelAccount = $this->loadModelByPk($id_cpanel_account, 'CpanelAccounts');
			$whmaccount = $this->loadModelByPk($cpanelAccount->id_account, 'WhmAccount');
			$whmApi = new WhmApi($whmaccount);
			if($ret = $whmApi->AccountAuthByHash()){
				$passwd = $whmApi->changePass($cpanelAccount->username, $newpass, $change_mysql_pass);
				if($passwd === false){
					$passwd['rawout'] = $passwd['statusmsg'] = $whmApi->getLastError('changePass');
				}else{
					app()->__Whatever__Log->recordWhmAction(SystemLog::CP_CHPWD, $newpass, $whmaccount->id_account, $id_cpanel_account);
				}
			}
			
			if(!$ret){
				$errors = $whmApi->getErrors();
			}

			$this->_returnAjax(array(
				'result' => $ret,
				'errors' => $errors,
				'passwd' => $passwd,
				'time' => $now
			));
		}
		
		$cpanelAccountIds = app()->request->getParam('cpanel_accounts', null);
		$renderData = array(
			'cpanelaccountsDataSource' => array(),
			'cpanelaccountsCount' => 0,
		);
		if(!empty($cpanelAccountIds['checked'])){
			$renderData['cpanelaccountsDataSource'] = CHtml::listData(CpanelAccounts::model()->findAllByPk($cpanelAccountIds['checked']), 'id_cpanel_accounts', 'username');
			$renderData['cpanelaccountsCount'] = CpanelAccounts::model()->countByAttributes(array('id_cpanel_accounts' => $cpanelAccountIds['checked']));
		}
		
		$this->render('passwords', $renderData);
	}
	
	public function actionBandWidth()
	{
		$now = time();
		if(app()->request->isAjaxRequest){
			$id_cpanel_account = app()->request->getParam('id_cpanel_account', null);
			$cpanelAccount = $this->loadModelByPk($id_cpanel_account, 'CpanelAccounts');
			$whmaccount = $this->loadModelByPk($cpanelAccount->id_account, 'WhmAccount');
			$whmApi = new WhmApi($whmaccount);
			$ret = false;
			$bwresult = $errors = null;
			if($ret = $whmApi->AccountAuthByHash()){
				if(!app()->request->isPostRequest){
					$bandWidth = $whmApi->getBandWidth($cpanelAccount->username);
					$this->_returnAjax($bandWidth);
				}else{
					$newbw = app()->request->getPost('newbw', null);
					
					$bwresult = $whmApi->limitBandWidth($cpanelAccount->username, $newbw);
					if($bwresult === false){
						$bwresult['rawout'] = $bwresult['statusmsg'] = $whmApi->getLastError('limitBandWidth');
					}else{
						app()->__Whatever__Log->recordWhmAction(SystemLog::CP_BWL, $newbw . 'mb', $whmaccount->id_account, $id_cpanel_account);
					}
				}
			}
			if(!$ret){
				$errors = $whmApi->getErrors();
			}

			$this->_returnAjax(array(
				'result' => $ret,
				'errors' => $errors,
				'bwresult' => $bwresult,
				'time' => $now
			));
		}
		
		$cpanelAccountIds = app()->request->getParam('cpanel_accounts', null);
		$renderData = array(
			'cpanelaccountsDataSource' => array(),
			'cpanelaccountsCount' => 0,
		);
		if(!empty($cpanelAccountIds['checked'])){
			$renderData['cpanelaccountsDataSource'] = CHtml::listData(CpanelAccounts::model()->findAllByPk($cpanelAccountIds['checked']), 'id_cpanel_accounts', 'username');
			$renderData['cpanelaccountsCount'] = CpanelAccounts::model()->countByAttributes(array('id_cpanel_accounts' => $cpanelAccountIds['checked']));
			$cpanelAccountsData = CpanelAccounts::model()->findAllByPk($cpanelAccountIds['checked']);
			if(empty($cpanelAccountsData[0]->WhmAccount->AccountFunctions) || $cpanelAccountsData[0]->WhmAccount->AccountFunctions->modifyacct != 1){
				$renderData['denyChangebwAccount'] = $cpanelAccountsData[0]->WhmAccount;
			}
		}
		
		$this->render('bandwidth', $renderData);
	}
	
	public function actionSuspend()
	{
		$now = time();
		if(app()->request->isAjaxRequest){
			$id_cpanel_account = app()->request->getParam('id_cpanel_account', null);
			$cpanelAccount = $this->loadModelByPk($id_cpanel_account, 'CpanelAccounts');
			$whmaccount = $this->loadModelByPk($cpanelAccount->id_account, 'WhmAccount');
			$whmApi = new WhmApi($whmaccount);
			$ret = false;
			$suspendresult = $errors = null;
			if($ret = $whmApi->AccountAuthByHash()){
				$reason = app()->request->getPost('reason', null);
				
				$suspendresult = $whmApi->suspend($cpanelAccount->username, $reason);
				if($suspendresult === false){
					$suspendresult['statusmsg'] = $whmApi->getLastError('suspend');
				}			
			}
			if(!$ret){
				$errors = $whmApi->getErrors();
			}else{
				app()->__Whatever__Log->recordWhmAction(SystemLog::CP_SUSPENDACC, $reason, $whmaccount->id_account, $id_cpanel_account);
			}

			$this->_returnAjax(array(
				'result' => $ret,
				'errors' => $errors,
				'suspendresult' => $suspendresult,
				'time' => $now
			));
		}
		
		$cpanelAccountIds = app()->request->getParam('cpanel_accounts', null);
		$renderData = array(
			'cpanelaccountsDataSource' => array(),
			'cpanelaccountsCount' => 0,
		);
		if(!empty($cpanelAccountIds['checked'])){
			$renderData['cpanelaccountsDataSource'] = CHtml::listData(CpanelAccounts::model()->findAllByPk($cpanelAccountIds['checked']), 'id_cpanel_accounts', 'username');
			$renderData['cpanelaccountsCount'] = CpanelAccounts::model()->countByAttributes(array('id_cpanel_accounts' => $cpanelAccountIds['checked']));
			$cpanelAccountsData = CpanelAccounts::model()->findAllByPk($cpanelAccountIds['checked']);
			if(empty($cpanelAccountsData[0]->WhmAccount->AccountFunctions) || $cpanelAccountsData[0]->WhmAccount->AccountFunctions->suspendacct != 1){
				$renderData['denySuspendAccount'] = $cpanelAccountsData[0]->WhmAccount;
			}
		}
		
		$this->render('suspend', $renderData);
	}
	
	public function actionUnsuspend()
	{
		$now = time();
		if(app()->request->isAjaxRequest){
			$id_cpanel_account = app()->request->getParam('id_cpanel_account', null);
			$cpanelAccount = $this->loadModelByPk($id_cpanel_account, 'CpanelAccounts');
			$whmaccount = $this->loadModelByPk($cpanelAccount->id_account, 'WhmAccount');
			$whmApi = new WhmApi($whmaccount);
			$ret = false;
			$unsuspendresult = $errors = null;
			if($ret = $whmApi->AccountAuthByHash()){
				$unsuspendresult = $whmApi->unSuspend($cpanelAccount->username);
				if($unsuspendresult === false){
					$unsuspendresult['statusmsg'] = $whmApi->getLastError('unSuspend');
				}			
			}
			if(!$ret){
				$errors = $whmApi->getErrors();
			}else{
				app()->__Whatever__Log->recordWhmAction(SystemLog::CP_UNSUSPENDACC, $cpanelAccount->domain, $whmaccount->id_account, $id_cpanel_account);
			}

			$this->_returnAjax(array(
				'result' => $ret,
				'errors' => $errors,
				'unsuspendresult' => $unsuspendresult,
				'time' => $now
			));
		}
		
		$cpanelAccountIds = app()->request->getParam('cpanel_accounts', null);
		$renderData = array(
			'cpanelaccountsDataSource' => array(),
			'cpanelaccountsCount' => 0,
		);
		if(!empty($cpanelAccountIds['checked'])){
			$renderData['cpanelaccountsDataSource'] = CHtml::listData(CpanelAccounts::model()->findAllByPk($cpanelAccountIds['checked']), 'id_cpanel_accounts', 'username');
			$renderData['cpanelaccountsCount'] = CpanelAccounts::model()->countByAttributes(array('id_cpanel_accounts' => $cpanelAccountIds['checked']));
			$cpanelAccountsData = CpanelAccounts::model()->findAllByPk($cpanelAccountIds['checked']);
			if(empty($cpanelAccountsData[0]->WhmAccount->AccountFunctions) || $cpanelAccountsData[0]->WhmAccount->AccountFunctions->suspendacct != 1){
				$renderData['denySuspendAccount'] = $cpanelAccountsData[0]->WhmAccount;
			}
		}
		
		$this->render('unsuspend', $renderData);
	}
	
	public function actionTerminate()
	{
		$now = time();
		if(app()->request->isAjaxRequest){
			$id_cpanel_account = app()->request->getParam('id_cpanel_account', null);
			$cpanelAccount = $this->loadModelByPk($id_cpanel_account, 'CpanelAccounts');
			$whmaccount = $this->loadModelByPk($cpanelAccount->id_account, 'WhmAccount');
			$whmApi = new WhmApi($whmaccount);
			$ret = false;
			$terminateresult = $errors = null;
			if($ret = $whmApi->AccountAuthByHash()){
				$keepdns = app()->request->getPost('keepdns', null);
				
				$terminateresult = $whmApi->terminate($cpanelAccount->username, $keepdns);
				if($terminateresult === false){
					$terminateresult['rawout'] = $terminateresult['statusmsg'] = $whmApi->getLastError('terminate');
				}
				if($terminateresult->status == 1){
					WhmAccount::model()->deleteAllByAttributes(array('username' => $cpanelAccount->username));
					$cpanelAccount->delete();
				}
			}
			if(!$ret){
				$errors = $whmApi->getErrors();
			}else{
				app()->__Whatever__Log->recordWhmAction(SystemLog::CP_TERMIACC, $keepdns? 'keepdns' : '', $whmaccount->id_account, $id_cpanel_account);
			}

			$this->_returnAjax(array(
				'result' => $ret,
				'errors' => $errors,
				'terminateresult' => $terminateresult,
				'time' => $now
			));
		}
		
		$cpanelAccountIds = app()->request->getParam('cpanel_accounts', null);
		$renderData = array(
			'cpanelaccountsDataSource' => array(),
			'cpanelaccountsCount' => 0,
		);
		if(!empty($cpanelAccountIds['checked'])){
			$renderData['cpanelaccountsDataSource'] = CHtml::listData(CpanelAccounts::model()->findAllByPk($cpanelAccountIds['checked']), 'id_cpanel_accounts', 'username');
			$renderData['cpanelaccountsCount'] = CpanelAccounts::model()->countByAttributes(array('id_cpanel_accounts' => $cpanelAccountIds['checked']));
			$cpanelAccountsData = CpanelAccounts::model()->findAllByPk($cpanelAccountIds['checked']);
			if(empty($cpanelAccountsData[0]->WhmAccount->AccountFunctions) || $cpanelAccountsData[0]->WhmAccount->AccountFunctions->terminatereseller != 1){
				$renderData['denySuspendAccount'] = $cpanelAccountsData[0]->WhmAccount;
			}
		}
		
		$this->render('terminate', $renderData);
	}
	
	public function actionGetAccountSummary()
	{
		if(app()->request->isAjaxRequest){
			$id_cpanel_account = app()->request->getParam('id_cpanel_account', null);
			$cpanelAccount = $this->loadModelByPk($id_cpanel_account, 'CpanelAccounts');
			$whmaccount = $this->loadModelByPk($cpanelAccount->id_account, 'WhmAccount');
			$whmApi = new WhmApi($whmaccount);
			if($ret = $whmApi->AccountAuthByHash()){
				$this->_returnAjax(
					$whmApi->getAccountSummary($cpanelAccount->username)
				);
			}
		}
	}
	
	public function actionModify()
	{
		$now = time();
		if(app()->request->isAjaxRequest){
			$id_cpanel_account = app()->request->getParam('id_cpanel_account', null);
			$cpanelAccount = $this->loadModelByPk($id_cpanel_account, 'CpanelAccounts');
			$whmaccount = $this->loadModelByPk($cpanelAccount->id_account, 'WhmAccount');
			$whmApi = new WhmApi($whmaccount);
			$ret = false;
			$modifyresult = $errors = null;
			if($ret = $whmApi->AccountAuthByHash()){
				$modifyVariables = app()->request->getPost('modifyVariables', null);
				
				$modifyresult = $whmApi->AccountModify($cpanelAccount->username, $modifyVariables);
				if($modifyresult === false){
					$modifyresult['statusmsg'] = $whmApi->getLastError('AccountModify');
				}else{
					app()->__Whatever__Log->recordWhmAction(SystemLog::CP_MODIACC, $cpanelAccount->domain, $whmaccount->id_account, $id_cpanel_account);
				}		
			}
			if(!$ret){
				$errors = $whmApi->getErrors();
			}

			$this->_returnAjax(array(
				'result' => $ret,
				'errors' => $errors,
				'modifyresult' => $modifyresult,
				'time' => $now
			));
		}
		
		$cpanelAccountIds = app()->request->getParam('cpanel_accounts', null);
		$renderData = array(
			'cpanelaccountsDataSource' => array(),
			'cpanelaccountsCount' => 0,
		);
		if(!empty($cpanelAccountIds['checked'])){
			$renderData['cpanelaccountsDataSource'] = CHtml::listData(CpanelAccounts::model()->findAllByPk($cpanelAccountIds['checked']), 'id_cpanel_accounts', 'username');
			$renderData['cpanelaccountsCount'] = CpanelAccounts::model()->countByAttributes(array('id_cpanel_accounts' => $cpanelAccountIds['checked']));
			$cpanelAccountsData = CpanelAccounts::model()->findAllByPk($cpanelAccountIds['checked']);
			if(empty($cpanelAccountsData[0]->WhmAccount->AccountFunctions) || $cpanelAccountsData[0]->WhmAccount->AccountFunctions->modifyacct != 1){
				$renderData['denySuspendAccount'] = $cpanelAccountsData[0]->WhmAccount;
			}
		}
		
		$this->render('modify', $renderData);
	}
	
	public function accessRules()
	{
		return array(
			array(
				'allow',
				'actions' => array('searchresult'),
				'expression' => 'app()->user->isLogin() && (app()->user->hasManageAccountPerm() || app()->user->hasManageBackupPerm())',
				'redirect' => array('default/noaccess')
			),
		
			array(
				'allow',
				'actions' => array('index', 'detail', 'getaccountsummary', 'modify', 'passwords', 'bandwidth', 'suspend', 'unsuspend', 'terminate'),
				'expression' => 'app()->user->isLogin() && app()->user->hasManageAccountPerm()',
				'redirect' => array('default/noaccess')
			),

			array(
				'deny',	//should be 'deny', 'allow' is used to give convenient to make test
				'users' => array('*'),
				'redirect' => array('user/login')
			)
		);
	}
}